package com.purchasing.system.repository;

import java.time.LocalDateTime;
import java.util.List;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import com.purchasing.system.model.Notification;
import com.purchasing.system.model.User;

@Repository
public interface NotificationRepository extends JpaRepository<Notification, Long> {
    Page<Notification> findByUser(User user, Pageable pageable);
    
    List<Notification> findByUserAndReadOrderByCreatedAtDesc(User user, boolean read);
    
    long countByUserAndRead(User user, boolean read);
    
    @Query("SELECT n FROM Notification n WHERE n.user = :user " +
           "AND (:type IS NULL OR n.type = :type) " +
           "AND (:read IS NULL OR n.read = :read) " +
           "AND (:startDate IS NULL OR n.createdAt >= :startDate) " +
           "AND (:endDate IS NULL OR n.createdAt <= :endDate)")
    Page<Notification> findByUserWithFilters(
            @Param("user") User user, 
            @Param("type") Notification.NotificationType type, 
            @Param("read") Boolean read, 
            @Param("startDate") LocalDateTime startDate, 
            @Param("endDate") LocalDateTime endDate, 
            Pageable pageable);
} 